package com.jishuo.pzk.service;

import com.google.gson.Gson;
import com.jishuo.base.config.PzkConfig;
import com.jishuo.pzk.bean.base.VouFieldVO;
import com.jishuo.pzk.bean.base.VoucherDetailVO;
import com.jishuo.pzk.bean.base.VoucherVO;
import com.jishuo.pzk.bean.bill.StudentVO;
import com.jishuo.pzk.util.ImageUtils;
import com.jishuo.pzk.util.VoucherUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.awt.*;
import java.util.ArrayList;
import java.util.List;

/**
 * 学生缴费
 */
@Service
public class StudentService {
    private static final String vocde = PzkConfig.Vocode_Student;
    private static final int busitype = PzkConfig.Pztype_Student;//凭证类型

    @Resource
    private VoucherService voucherService;

    @Resource
    private VoucherUtil voucherUtil;

    /**
     * 存储凭证数据
     * @param data 报文
     * @return
     */
    public int savePzk(String data) {
        StudentVO student = new Gson().fromJson(data, StudentVO.class);
        VoucherVO voucher = voucherUtil.getVoucher(busitype, "0", vocde, student.getStudentNum(), "0", "2", student.getCantonId(), student.getBankId(), "0", student.getBankStation(), student.getPayBankDt(), data);
        return voucherService.saveVoucher(voucher);
    }

    /**
     * 获得凭证字段明细
     * @param list 字段定义
     * @param VO_SN
     * @param ORDERBILLSN 票号
     * @param data 报文
     * @return
     */
    public static List<VoucherDetailVO> getDetail(List<VouFieldVO> list, VoucherVO voucher) {
        String VO_SN = voucher.getVoSn();
        String ORDERBILLSN = voucher.getOrderbillsn();
        StudentVO vo = new Gson().fromJson(voucher.getVoucher(), StudentVO.class);
        List<VoucherDetailVO> listDe = new ArrayList<>();

        for (VouFieldVO vouFieldVO : list) {
            int x;
            int y;
            //int x1;
            String pos = "";

            if ("left".equals(vouFieldVO.getHalign()) || vouFieldVO.getHalign() == null) {
                pos = vouFieldVO.getFieldPosLeft();

            } else if ("right".equals(vouFieldVO.getHalign())) {
                pos = vouFieldVO.getFieldPosRight();

            } else if ("center".equals(vouFieldVO.getHalign())) {
                pos = vouFieldVO.getFieldPosLeft();
                // x1=Integer.parseInt(pos1.split(",")[0]);
            }
            if (vouFieldVO.getField() != null) {

                if (vouFieldVO.getField().trim().equals("mane")) {

                    VoucherDetailVO voucherDetailVO = new VoucherDetailVO();
                    voucherDetailVO.setVoDeSn(VO_SN);
                    voucherDetailVO.setOrderbillsn(ORDERBILLSN);

                    Color color = Color.BLACK;
                    String cc = ImageUtils.Color2String(color);
                    x = Integer.parseInt(pos.split(",")[0]);
                    y = Integer.parseInt(pos.split(",")[1]);

                    voucherDetailVO.setAlpha(1);
                    voucherDetailVO.setFontcolor(cc);
                    voucherDetailVO.setFontname("宋体");

                    voucherDetailVO.setFontsize(40);
                    voucherDetailVO.setHalign(vouFieldVO.getHalign());
                    voucherDetailVO.setX(x);
                    voucherDetailVO.setY(y);
                    voucherDetailVO.setField("mane");
                    voucherDetailVO.setFieldType("String");
                    voucherDetailVO.setFieldValue(vo.getStudentName());
                    voucherDetailVO.setMaxlength(vouFieldVO.getMaxlength());
                    voucherDetailVO.setIsnewline(vouFieldVO.getIsnewline());
                    listDe.add(voucherDetailVO);

                }
                if (vouFieldVO.getField().trim().equals("studentsn")) {

                    VoucherDetailVO voucherDetailVO = new VoucherDetailVO();
                    voucherDetailVO.setVoDeSn(VO_SN);
                    voucherDetailVO.setOrderbillsn(ORDERBILLSN);

                    Color color = Color.BLACK;
                    String cc = ImageUtils.Color2String(color);
                    x = Integer.parseInt(pos.split(",")[0]);
                    y = Integer.parseInt(pos.split(",")[1]);

                    voucherDetailVO.setAlpha(1);
                    voucherDetailVO.setFontcolor(cc);
                    voucherDetailVO.setFontname("宋体");

                    voucherDetailVO.setFontsize(40);
                    voucherDetailVO.setHalign(vouFieldVO.getHalign());
                    voucherDetailVO.setX(x);
                    voucherDetailVO.setY(y);
                    voucherDetailVO.setField("studentsn");
                    voucherDetailVO.setFieldType("String");
                    voucherDetailVO.setFieldValue(vo.getStudentNum());
                    voucherDetailVO.setMaxlength(vouFieldVO.getMaxlength());
                    voucherDetailVO.setIsnewline(vouFieldVO.getIsnewline());
                    listDe.add(voucherDetailVO);

                }
                if (vouFieldVO.getField().trim().equals("classname")) {

                    VoucherDetailVO voucherDetailVO = new VoucherDetailVO();
                    voucherDetailVO.setVoDeSn(VO_SN);
                    voucherDetailVO.setOrderbillsn(ORDERBILLSN);

                    Color color = Color.BLACK;
                    String cc = ImageUtils.Color2String(color);
                    x = Integer.parseInt(pos.split(",")[0]);
                    y = Integer.parseInt(pos.split(",")[1]);

                    voucherDetailVO.setAlpha(1);
                    voucherDetailVO.setFontcolor(cc);
                    voucherDetailVO.setFontname("宋体");

                    voucherDetailVO.setFontsize(40);
                    voucherDetailVO.setHalign(vouFieldVO.getHalign());
                    voucherDetailVO.setX(x);
                    voucherDetailVO.setY(y);
                    voucherDetailVO.setField("classname");
                    voucherDetailVO.setFieldType("String");
                    voucherDetailVO.setFieldValue(vo.getClassName());
                    voucherDetailVO.setMaxlength(vouFieldVO.getMaxlength());
                    voucherDetailVO.setIsnewline(vouFieldVO.getIsnewline());
                    listDe.add(voucherDetailVO);

                }
                if (vouFieldVO.getField().trim().equals("bankstation")) {

                    VoucherDetailVO voucherDetailVO = new VoucherDetailVO();
                    voucherDetailVO.setVoDeSn(VO_SN);
                    voucherDetailVO.setOrderbillsn(ORDERBILLSN);

                    Color color = Color.BLACK;
                    String cc = ImageUtils.Color2String(color);
                    x = Integer.parseInt(pos.split(",")[0]);
                    y = Integer.parseInt(pos.split(",")[1]);

                    voucherDetailVO.setAlpha(1);
                    voucherDetailVO.setFontcolor(cc);
                    voucherDetailVO.setFontname("宋体");

                    voucherDetailVO.setFontsize(40);
                    voucherDetailVO.setHalign(vouFieldVO.getHalign());
                    voucherDetailVO.setX(x);
                    voucherDetailVO.setY(y);
                    voucherDetailVO.setField("bankstation");
                    voucherDetailVO.setFieldType("String");
                    voucherDetailVO.setFieldValue(vo.getBankName());
                    voucherDetailVO.setMaxlength(vouFieldVO.getMaxlength());
                    voucherDetailVO.setIsnewline(vouFieldVO.getIsnewline());
                    listDe.add(voucherDetailVO);

                }
                if (vouFieldVO.getField().trim().equals("studentnum")) {

                    VoucherDetailVO voucherDetailVO = new VoucherDetailVO();
                    voucherDetailVO.setVoDeSn(VO_SN);
                    voucherDetailVO.setOrderbillsn(ORDERBILLSN);

                    Color color = Color.BLACK;
                    String cc = ImageUtils.Color2String(color);
                    x = Integer.parseInt(pos.split(",")[0]);
                    y = Integer.parseInt(pos.split(",")[1]);

                    voucherDetailVO.setAlpha(1);
                    voucherDetailVO.setFontcolor(cc);
                    voucherDetailVO.setFontname("宋体");

                    voucherDetailVO.setFontsize(40);
                    voucherDetailVO.setHalign(vouFieldVO.getHalign());
                    voucherDetailVO.setX(x);
                    voucherDetailVO.setY(y);
                    voucherDetailVO.setField("studentnum");
                    voucherDetailVO.setFieldType("String");
                    voucherDetailVO.setFieldValue(vo.getPayerAccount());
                    voucherDetailVO.setMaxlength(vouFieldVO.getMaxlength());
                    voucherDetailVO.setIsnewline(vouFieldVO.getIsnewline());
                    listDe.add(voucherDetailVO);

                }
                if (vouFieldVO.getField().trim().equals("bankaccount")) {

                    VoucherDetailVO voucherDetailVO = new VoucherDetailVO();
                    voucherDetailVO.setVoDeSn(VO_SN);
                    voucherDetailVO.setOrderbillsn(ORDERBILLSN);

                    Color color = Color.BLACK;
                    String cc = ImageUtils.Color2String(color);
                    x = Integer.parseInt(pos.split(",")[0]);
                    y = Integer.parseInt(pos.split(",")[1]);

                    voucherDetailVO.setAlpha(1);
                    voucherDetailVO.setFontcolor(cc);
                    voucherDetailVO.setFontname("宋体");

                    voucherDetailVO.setFontsize(40);
                    voucherDetailVO.setHalign(vouFieldVO.getHalign());
                    voucherDetailVO.setX(x);
                    voucherDetailVO.setY(y);
                    voucherDetailVO.setField("bankaccount");
                    voucherDetailVO.setFieldType("String");
                    voucherDetailVO.setFieldValue(vo.getAccountCode());
                    voucherDetailVO.setMaxlength(vouFieldVO.getMaxlength());
                    voucherDetailVO.setIsnewline(vouFieldVO.getIsnewline());
                    listDe.add(voucherDetailVO);

                }

                if (vouFieldVO.getField().trim().equals("total")) {

                    VoucherDetailVO voucherDetailVO = new VoucherDetailVO();
                    voucherDetailVO.setVoDeSn(VO_SN);
                    voucherDetailVO.setOrderbillsn(ORDERBILLSN);

                    Color color = Color.BLACK;
                    String cc = ImageUtils.Color2String(color);
                    String[] posId = pos.split("\\|");
                    x = Integer.parseInt(posId[0].split(",")[0]);
                    y = Integer.parseInt(posId[0].split(",")[1]);

                    voucherDetailVO.setAlpha(1);
                    voucherDetailVO.setFontcolor(cc);
                    voucherDetailVO.setFontname("宋体");

                    voucherDetailVO.setFontsize(40);
                    voucherDetailVO.setHalign(vouFieldVO.getHalign());
                    voucherDetailVO.setX(x);
                    voucherDetailVO.setY(y);
                    voucherDetailVO.setField("total");
                    voucherDetailVO.setFieldType("String");
                    voucherDetailVO.setFieldValue(vo.getFund());
                    voucherDetailVO.setMaxlength(vouFieldVO.getMaxlength());
                    voucherDetailVO.setIsnewline(vouFieldVO.getIsnewline());
                    listDe.add(voucherDetailVO);
                }
            }
        }
        return listDe;
    }
}
